<template>
    <div class="p-2">
        <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
            :leave-active-class="proxy?.animate.searchAnimate.leave">
            <div class="search">
                <el-form :model="queryParams" ref="queryFormRef" :inline="true">
                    <el-form-item label="发货日期：" prop="timeDate">
                        <el-date-picker v-model="queryParams.timeDate" type="daterange" range-separator="至"
                            start-placeholder="开始时间" end-placeholder="结束时间" @change="handleTimeChange"
                            format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
                    </el-form-item>
                    <el-form-item label="客户：" prop="orderPrintStatus">
                        <el-select style="width: 120px;" v-model="queryParams.orderPrintStatus" clearable>
                            <el-option label="未打印" value="0"></el-option>
                            <el-option label="已打印" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="制单员：" prop="orderPrintStatus">
                        <el-select style="width: 120px;" v-model="queryParams.orderPrintStatus" clearable>
                            <el-option label="未打印" value="0"></el-option>
                            <el-option label="已打印" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="客户渠道：" prop="orderPrintStatus">
                        <el-select style="width: 120px;" v-model="queryParams.orderPrintStatus" clearable>
                            <el-option label="未打印" value="0"></el-option>
                            <el-option label="已打印" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="发货状态：" prop="orderPrintStatus">
                        <el-select style="width: 120px;" v-model="queryParams.orderPrintStatus" clearable>
                            <el-option label="未打印" value="0"></el-option>
                            <el-option label="已打印" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="对账状态：" prop="orderPrintStatus">
                        <el-select style="width: 120px;" v-model="queryParams.orderPrintStatus" clearable>
                            <el-option label="未打印" value="0"></el-option>
                            <el-option label="已打印" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="模糊查询：" prop="workName">
                        <el-input v-model="queryParams.workName" placeholder="请输入"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </transition>
        <el-card shadow="never">
            <template #header>
                <div style="display: flex; align-items: center; justify-content: space-between;">
                    <!-- <div>
                        <el-button type="success"> <i class="iconfont icon-daifukuan mr1"></i> 批量对账</el-button>
                    </div> -->
                    <div>
                        <strong class="mr20">订单数量：{{ orderQuantity }}</strong>
                        <strong>订单总价：{{ orderAmount }}</strong>
                    </div>
                </div>
            </template>
            <el-table v-loading="loading" :data="dataList">
                <el-table-column label="所属公司" prop="companyName" align="center"></el-table-column>
                <el-table-column label="制单员" prop="createByName" align="center"></el-table-column>
                <el-table-column label="发货日期" prop="deliveryDate" align="center"></el-table-column>
                <el-table-column label="送货单号" prop="oddNumbers" align="center"></el-table-column>
                <el-table-column label="客户渠道" prop="customerSource" align="center"></el-table-column>
                <el-table-column label="客户名称" prop="customerName" align="center"></el-table-column>
                <el-table-column label="产品名称明细" prop="productName" align="center" width="120px"></el-table-column>
                <el-table-column label="箱规" prop="boxGauge" align="center"></el-table-column>
                <el-table-column label="订单数量" prop="number" align="center"></el-table-column>
                <el-table-column label="单价" prop="bomPrice" align="center"></el-table-column>
                <el-table-column label="订单金额" prop="saleAmount" align="center"></el-table-column>
                <el-table-column label="发货状态" align="center">
                    <template #default="scope">
                        <el-tag v-if="scope.row.deliveryStatus == 0" type="danger">未发货</el-tag>
                        <el-tag v-else-if="scope.row.deliveryStatus == 1" type="success">已发货</el-tag>
                        <!-- <el-tag v-else-if="scope.row.deliveryStatus === 2" type="warning">部分发货</el-tag> -->
                    </template>
                </el-table-column>
                <el-table-column label="收款信息" prop="collectInfo" align="center"></el-table-column>
                <el-table-column label="对账状态" align="center">
                    <template #default="scope">
                        <el-tag v-if="scope.row.reconciliationStatus == 0" type="danger">未对账</el-tag>
                        <el-tag v-else-if="scope.row.reconciliationStatus == 1" type="warning">已对账</el-tag>
                        <el-tag v-else-if="scope.row.deliveryStatus == 2" type="success">已回款</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="120px">
                    <template #default="scope">
                        <el-tooltip content="对账" placement="top">
                            <el-button @click="handleOpenApply(scope.row)" link type="warning">
                                <i style="font-size: 24px;" class="iconfont icon-daifukuan"></i>
                            </el-button>
                        </el-tooltip>
                        <el-tooltip content="撤销对账">
                            <el-button @click="deleteList(scope.row)" link type="danger">
                                <i style="font-size: 24px;" class="iconfont icon-chehui-1"></i>
                            </el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>

            </el-table>
            <pagination v-show="totalData > 0" :total="totalData" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </el-card>

        <!-- 申请对账 -->
        <Reconciliation :visible="applyVisible" :initList="applyObj" @submit="getList" @close="applyVisible = false" />
    </div>
</template>
<script setup lang="ts">
import { type FormInstance } from 'element-plus';

import { getSalesReconciliationListApi, cancelReconciliationApi } from '@/api/sj/financialManagement/salesReconciliation'

/** 对账 */
import Reconciliation from './components/reconciliation.vue';
import useApplyForReconciliation from '@/hooks/purchaseManagement/toBeStored/useApplyForReconciliation';
const { applyVisible, applyObj, handleOpenApply } = useApplyForReconciliation();

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const queryFormRef = ref<FormInstance>();

const data = reactive<pageDataOnlyQuery<any>>({
    queryParams: {
        timeDate: '',
        startDate: '',
        endDate: '',
        pageSize: 10,
        pageNum: 1
    }
});

const { queryParams } = toRefs(data);

const loading = ref(false);
const dataList = ref<any>([]);
const totalData = ref(0);
const orderAmount = ref(0);
const orderQuantity = ref(0);

const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};

const resetQuery = () => {
    queryFormRef.value?.resetFields();
    queryParams.value.startDate = '';
    queryParams.value.endDate = '';
    queryParams.value.timeDate = '';
    handleQuery();
};

/** 日期选择 */
const handleTimeChange = (val: any) => {
    queryParams.value.startDate = val[0];
    queryParams.value.endDate = val[1];
};

/** 列表删除 */
const deleteList = (row: any) => {
    // 材料已入库或者部分入库，您无法制除该亲记录

    if (row.reconciliationStatus != 1) return ElMessage({
        type: 'warning',
        message: '该订单未对账，无法撤销！'
    });

    ElMessageBox.confirm('撤销后此订单将回到未对账状态，您确定么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    })
        .then(async () => {
            try {
                const { code } = await cancelReconciliationApi({ id: row.id });

                if (code == 200) {
                    ElMessage({
                        type: 'success',
                        message: '撤销成功!'
                    });

                    getList();
                }
            } catch (error) {

            }

        })
        .catch(() => { });
};

const getList = async () => {
    loading.value = true;
    try {
        const { data, code } = await getSalesReconciliationListApi(queryParams.value);
        if (code === 200) {
            dataList.value = data.dataPage.records;
            totalData.value = data.dataPage.total;
            orderQuantity.value = data.total
            orderAmount.value = data.orderAmount;
        }
        loading.value = false;
    } catch (error) {
        loading.value = false;
    }
};

onMounted(() => {
    getList();
});

</script>
<style lang="scss" scoped></style>